#include <gulcolor.h>
#include <S32STOR.H>
#include <s32file.h>
#include <centralrepository.h>
#include <ProfileEngineSDKCRKeys.h>

#include "Utils.h"
#include "Uids.h"

void Utils::SaveIntL(CDictionaryStore* aIniFile, const TInt aKey, const TInt aValue)
{
	RDictionaryWriteStream stream;
	stream.AssignLC(*aIniFile, TUid::Uid(aKey));
	stream.WriteInt32L(aValue);
	stream.CommitL();
	CleanupStack::PopAndDestroy(); // stream
	aIniFile->CommitL();
}

void Utils::SaveTimeL(CDictionaryStore* aIniFile, const TInt aKey, const TTime& aValue)
{
	RDictionaryWriteStream stream;
	stream.AssignLC(*aIniFile, TUid::Uid(aKey));//+stream
	TUint32 high = I64HIGH(aValue.Int64());
	TUint32 low = I64LOW(aValue.Int64());
	stream.WriteUint32L(high);
	stream.WriteUint32L(low);
	stream.CommitL();
	CleanupStack::PopAndDestroy(); //-stream
	aIniFile->CommitL();
}

void Utils::LoadIntL(CDictionaryStore* aIniFile, const TInt aKey, TInt& aValue, TInt aDefaultValue)
{
	aValue = aDefaultValue;
	TUid uid = TUid::Uid(aKey);
	if (aIniFile->IsPresentL(uid))
	{
		RDictionaryReadStream stream;
		stream.OpenLC(*aIniFile, uid);
		aValue = stream.ReadInt32L();
		CleanupStack::PopAndDestroy(); // stream
	}
}

void Utils::LoadPeriodL(CDictionaryStore* aIniFile, const TInt aKey, TTimeIntervalSeconds& aValue, TInt aDefaultValue)
{
	aValue = aDefaultValue;
	TUid uid = TUid::Uid(aKey);
	if (aIniFile->IsPresentL(uid))
	{
		RDictionaryReadStream stream;
		stream.OpenLC(*aIniFile, uid);
		aValue = stream.ReadInt32L();
		CleanupStack::PopAndDestroy(); // stream
	}
}

void Utils::LoadTimeL(CDictionaryStore* aIniFile, const TInt aKey, TTime& aValue)
{
	TUid uid = TUid::Uid(aKey);

	if (aIniFile->IsPresentL(uid))
	{
		RDictionaryReadStream stream;
		stream.OpenLC(*aIniFile, uid);//+stream
		TUint32 high, low;
		high = stream.ReadUint32L();
		low = stream.ReadUint32L();
		aValue = MAKE_TINT64(high, low);
		CleanupStack::PopAndDestroy(); //-stream
	}
}

void Utils::SaveDesL(CDictionaryStore *aIniFile, const TInt aKey, const TDesC& aValue)
{
	RDictionaryWriteStream stream;
	stream.AssignLC(*aIniFile, TUid::Uid(aKey));
	stream << aValue;
	stream.CommitL();

	CleanupStack::PopAndDestroy(); // stream
	aIniFile->CommitL();
}

void Utils::LoadDesL(CDictionaryStore *aIniFile, const TInt aKey, TDes& aValue)
{
	TUid uid = TUid::Uid(aKey);
	if (aIniFile->IsPresentL(uid))
	{
		RDictionaryReadStream stream;
		stream.OpenLC(*aIniFile, uid);
		stream >> aValue;
		CleanupStack::PopAndDestroy(); // stream
	}
}

TPtrC Utils::ExtractName(const TDesC& aFileName)
{
	TInt pos = aFileName.LocateReverse('\\');
	return aFileName.Mid(pos+1);
}

TInt Utils::GetActiveProfileId()
{
	//get profileId
	TInt profileId = 0;
	CRepository* cr = CRepository::NewLC(KCRUidProfileEngine);//+cr
	cr->Get(KProEngActiveProfile, profileId);
	CleanupStack::PopAndDestroy();//-cr
	return profileId;
}

TInt Utils::GetActiveVolumeL()
{
	TInt volume = 1;
	CRepository* cr = CRepository::NewLC(KCRUidProfileEngine);//+cr
	cr->Get(KProEngActiveRingingVolume, volume);
	CleanupStack::PopAndDestroy();//-cr
	return volume;
}
